package com.qfedu.lc.user.listener;

import cn.hutool.core.util.RandomUtil;
import com.qfedu.lc.common.config.RabbitKeyConfig;
import com.qfedu.lc.common.config.RedisKeyConfig;
import com.qfedu.lc.common.param.MqMsg;
import com.qfedu.lc.core.sms.AliSmsUtil;
import com.qfedu.lc.user.entity.SmsLog;
import com.qfedu.lc.user.mapper.SmsLogMapper;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisKeyValueTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * --- 年薪百万 ---
 * ---So easy---
 *
 * @author ZhiHuiJun
 * @motto 学习都是人上人
 * @desc 就是很帅很优秀
 * @CC 每天漂亮、幸福、快乐、自由
 */
@Component
public class SmsRabbitListener {
    /**
     * 队列的消费者
     */
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private SmsLogMapper smsLogMapper;

    @RabbitListener(queues = RabbitKeyConfig.Q_SENDSMS)
    public void smsListener(MqMsg msg) {

        System.out.println("消费者得到消息  msg = " + msg);

        //1.生成随机验证码
        String code = RandomUtil.randomInt(1000, 9999) + "";

        //2.发送阿里云短信
        String phone = (String) msg.getMsg();
        System.out.println("phone = " + phone + "---验证码 = " + code);
        boolean result = AliSmsUtil.sendCode(phone, code);
        if (true) {
            switch (msg.getType()) {
                case 1:
                    stringRedisTemplate.opsForValue().set(RedisKeyConfig.SMS_RCODE+phone,code,RedisKeyConfig.SMS_RCODE_TIME, TimeUnit.SECONDS);
                    // 4.手机号验证码记录日志表
                    smsLogMapper.insert(new SmsLog(phone,"注册验证码",phone+"注册-验证码："+code,new Date()));
                    break;

                // 2.登录验证码
                case  2:

                    break;

                //3.密码找回验证码
                case  3:

                    break;
                default:
                    System.out.println("其他消息处理");
                    break;
            }
        }

    }
}
